﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NET4.DataNET4;
using System.IO;
using NET4.DataNET4.Repository;

namespace NET4.Programma
{
  public partial class DetailOfferRequest : Form
  {
    #region Delegates and Events
    public delegate void OfferRequestChangedHandler(object sender, OfferRequestEventArgs e);
    public event OfferRequestChangedHandler OfferRequestChanged;
    #endregion

    #region Properties
    private OfferteAanvraag offerRequest;
    public OfferteAanvraag OfferRequest
    {
      get
      {
        if (this.offerRequest == null) { this.offerRequest = new OfferteAanvraag(); }
        return this.offerRequest;
      }
      set
      {
        this.offerRequest = value;
      }
    }
    #endregion

    public DetailOfferRequest()
    {
      InitializeComponent();
    }

    #region Private methods
    private Byte[] getByteArrayFromFile(String file)
    {
      Byte[] buffer = null;

      try
      {
        FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
        BinaryReader br = new BinaryReader(fs);
        long numberOfBytes = new FileInfo(file).Length;
        buffer = br.ReadBytes((Int32)numberOfBytes);
      }
      catch (Exception) { }

      return buffer;
    }
    #endregion

    #region Protected methods
    protected void OnOfferRequestChanged()
    {
      if (this.OfferRequestChanged != null)
      {
        this.OfferRequestChanged(this, new OfferRequestEventArgs(this.OfferRequest));
      }
    }

    protected void Init()
    {
      this.txtQuantity.Text = this.OfferRequest.Aantal.ToString();
      this.txtDescription.Text = this.OfferRequest.Omschrijving;

      if (this.OfferRequest.UitersteReactieDatum != DateTime.MinValue)
      {
        this.dtpAnswerBeforeDate.Value = this.OfferRequest.UitersteReactieDatum;
      }

      if (this.OfferRequest.Afbeelding != null)
      {
        MemoryStream ms = new MemoryStream(this.OfferRequest.Afbeelding.ToArray());
        this.pbImage.Image = Image.FromStream(ms);

        this.btnDeleteImage.Enabled = true;
      }
      else
      {
        this.btnDeleteImage.Enabled = false;
      }
    }

    protected new Boolean Validate()
    {
      Int32 quantity = 0;
      if (!Int32.TryParse(this.txtQuantity.Text, out quantity) || quantity == 0)
      {
        this.epError.SetError(this.txtQuantity, "U heeft geen aantal ingevuld of in een onjuist formaat!");

        return false;
      }

      return true;
    }

    protected void Save()
    {
      Int32 quantity = 0;
      Int32.TryParse(this.txtQuantity.Text, out quantity);

      this.OfferRequest.Aantal = quantity;
      this.OfferRequest.Omschrijving = this.txtDescription.Text;
      this.OfferRequest.UitersteReactieDatum = this.dtpAnswerBeforeDate.Value;

      if (!String.IsNullOrEmpty(this.txtImage.Text) && File.Exists(this.txtImage.Text))
      {
        this.OfferRequest.Afbeelding = new System.Data.Linq.Binary(this.getByteArrayFromFile(this.txtImage.Text));
      }

      OfferteAanvraagRepository.Instance.Save(this.OfferRequest);
    }

    protected void DeleteImage()
    {
      this.OfferRequest.Afbeelding = null;

      this.pbImage.Image = null;
      this.btnDeleteImage.Enabled = false;
    }
    #endregion

    #region EventHandlers
    private void DetailOfferRequest_Load(object sender, EventArgs e)
    {
      this.Init();
    }

    private void btnImageBrowse_Click(object sender, EventArgs e)
    {
      if (this.ofdImage.ShowDialog() == DialogResult.OK)
      {
        this.txtImage.Text = this.ofdImage.FileName;
      }
    }

    private void txtImage_TextChanged(object sender, EventArgs e)
    {
      try
      {
        this.pbImage.Image = Image.FromFile(this.txtImage.Text);
        this.btnDeleteImage.Enabled = true;
      }
      catch (Exception)
      {
        this.pbImage.Image = null;
        this.btnDeleteImage.Enabled = false;
      }
    }

    private void btnDeleteImage_Click(object sender, EventArgs e)
    {
      if (MessageBox.Show("Weet u zeker dat u de afbeelding wilt verwijderen?", "Vraag", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
      {
        this.DeleteImage();
      }
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
      if (this.Validate())
      {
        this.Save();

        this.OnOfferRequestChanged();

        this.Close();
      }
    }
    #endregion

  }

  public class OfferRequestEventArgs : EventArgs
  {
    private OfferteAanvraag offerRequest;

    public OfferRequestEventArgs(OfferteAanvraag offerRequest)
      : base()
    {
      this.offerRequest = offerRequest;
    }

    public OfferteAanvraag OfferRequest
    {
      get { return this.offerRequest; }
    }
  }
}
